package com.maple.sdk.util;

import java.io.Serializable;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.SequenceGenerator;
import org.hibernate.type.Type;

/**
 * 只支持oracle和mysql
 *
 */
public class CommonIdGenerator extends IdentityGenerator implements Configurable {
	private boolean isMysql;
	private SequenceGenerator sequenceGenerator;

	public Serializable generate(SessionImplementor session, Object object)
			throws HibernateException {
		if (isMysql) {
			return super.generate(session, object);
		} else {
			return sequenceGenerator.generate(session, object);
		}
	}

	public void configure(Type type, Properties params, Dialect d)
			throws MappingException {
		isMysql= d instanceof MySQLDialect;
		if (!isMysql) {
			sequenceGenerator = new SequenceGenerator();
			sequenceGenerator.configure(type, params, d);
		}
	}
	
}
